package me.shiqiang.im;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;


@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
    @Value("${spring.redis.groupInfo.cache.expireTime}")
    Long groupInfoExpireTime;

    @Value("${spring.redis.userInfo.cache.expireTime}")
    Long userInfoExpireTime;

    @Primary
    @Bean
    public CacheManager GroupCacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        cacheManager.setDefaultExpiration(groupInfoExpireTime);
        return cacheManager;
    }

    @Bean
    public CacheManager UserCacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        cacheManager.setDefaultExpiration(userInfoExpireTime);
        return cacheManager;
    }
}
